$(function () {
    netty_query_init();
});
/**
 * 初始化方法
 */
function netty_query_init() {
    /////////////////////////////////下拉列表/////////////////////////
    $('.selectpicker').selectpicker();
    ////////////////////////初始化页面元素事件  start/////////////////////"
    common.timeselectInit($("#btn-time"), netty_query_updateData);//初始化时间控件
    $("#btn-query").click(function () {
        netty_query_updateData();
    });
    // 批量下载数据
    $('#netty-download').click(function () {
        downloadFiles();
    });

    // 批量重传数据
    $('#netty-upload').click(function () {
        uploadFiles();
        // 刷新表格
        netty_query_updateData();
    });

    ////////////////////////初始化页面元素事件  end/////////////////////"
    //初始化系统树
    nettyTreeInit($('#netty_tree'), netty_query_updateData, "type-fs-metadata-fastdfs")
    ////////////////////////初始化表格.start/////////////////////
    $('#id_netty').bootstrapTable({
        data: [],
        pagination: true, //显示分页
        //	    onlyInfoPagination:true, // true 只显示总数 不显示分页
        pageSize: 10,              //设置当前页显示数目
        pageList: [5, 10, 15, 20, 50],
        //	    showFooter:true,
        showRefresh: true, // 设置刷新按钮
        showToggle: true, //
        showColumns: true, //
        striped: true, //
        //	    escape:true,
        //	    showHeader:true,        //是否显示表头
        search: false, //是否显示搜索框
        toolbar: '#toolbar', //toolbar
        columns: [
            {
                checkbox: true,
                formatter: function (value,row,index){

                }
            },
            {
                field: 'job',
                title: '操作',
                align: 'center',
                visible: true,
                sortable: true,
                formatter: function (value, row, index) {
                    var dfsId = row.dfsId;
                    if (dfsId != "") {
                        return [
                            '<a class="camera" href="javascript:void(0)" title="预览">',
                            '<i class="glyphicon glyphicon-camera"></i>',
                            '</a>',
                            '<a class="download" href="javascript:void(0)" title="下载">',
                            '<i class="glyphicon glyphicon-download"></i>',
                            '</a>'
                        ].join('');
                    }
                },
                events: {
                    'click .download': function (e, value, row, index) {
                        // 文件下载
                        var form = $("<form>");
                        form.attr('style', 'display:none');
                        form.attr('target', '');
                        form.attr('method', 'post');
                        form.attr('action', 'netty/downloadNettyFile.action');
                        var input1 = $('<input>');
                        input1.attr('type', 'hidden');
                        input1.attr('name', 'dfsId');
                        input1.attr('value', row.dfsId);
                        form.append(input1);
                        input1 = $('<input>');
                        input1.attr('type', 'hidden');
                        input1.attr('name', 'ext');
                        input1.attr('value', row.ext);
                        form.append(input1);
                        input1 = $('<input>');
                        input1.attr('type', 'hidden');
                        input1.attr('name', 'seqNo');
                        input1.attr('value', row.seqNo);
                        form.append(input1);
                        input1 = $('<input>');
                        input1.attr('type', 'hidden');
                        input1.attr('name', 'code');
                        input1.attr('value', row.code);
                        form.append(input1);
                        input1 = $('<input>');
                        input1.attr('type', 'hidden');
                        input1.attr('name', 'deviceId');
                        input1.attr('value', row.deviceId);
                        form.append(input1);
                        $('body').append(form);
                        form.submit();
                        form.remove();
                    },
                    'click .camera': function (e, value, row, index) {
                        // 查看文件内容
                        var dfsId = row.dfsId;
                        var code = row.code;
                        $.ajax({
                            url: 'netty/getFileContentById.action',
                            data: {
                                dfsId: dfsId,
                                code: code
                            },
                            type: 'post',
                            dataType: 'json',
                            success: function (data) {
                                if (data.flag == "true") {
                                    $('#netty-showFile-modal').modal();
                                    $("#netty-Modal-Label").html("文件内容");
                                    $('#netty-File-Content-Area').val(data.msg);
                                }
                            },
                            error: function () {
                                $('#netty-showFile-modal').modal();
                                $("#netty-Modal-Label").html("文件内容");
                                $("#netty-File-Content-Area").val("查找文件内容失败!!");
                            }
                        });
                    }
                }
            },
            {
                field: 'dfsId',
                title: 'fastDfsId',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'showTime',
                title: '生成时间',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'retransmissionTime',
                title: '重传时间',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'retransmissionCount',
                title: '重传次数',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'year',
                title: '年',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'month',
                title: '月',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'day',
                title: '日',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'sysName',
                title: '系统名',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'l1Name',
                title: 'l1Name',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'l2Name',
                title: 'l2Name',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'l3Name',
                title: 'l3Name',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'deviceId',
                title: '设备ID',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'code',
                title: '主题',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'seqNo',
                title: '文件序号',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'ext',
                title: '文件扩展名',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'fileSize',
                title: '文件大小',
                align: 'center',
                visible: true,
                sortable: true
            },
            {
                field: 'requestType',
                title: '请求了类型',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'requestSize',
                title: '请求大小',
                align: 'center',
                visible: false,
                sortable: true
            },
            {
                field: 'host',
                title: '主机',
                align: 'center',
                visible: false,
                sortable: true
            }
        ]
    });
    ////////////////////////初始化表格  end/////////////////////
}
//var sysName = "";
var deviceId = "";
var code = "";
function netty_query_updateData(deviceIdOut, codeOut) {
    /*if (sysNameOut != undefined) {
        sysName = sysNameOut;
    }*/
    if (deviceIdOut != undefined) {
        deviceId = deviceIdOut
    }
    if (codeOut != undefined) {
        code = codeOut
    }
    var times = $('#btn-time').html();
    var queryKeyWord = $('#ip_keyWord').val();
    var count = $('#s_netty_count_type').val();
    var url = 'netty/nettyQuery.action?deviceId=' + deviceId + "&code=" + code + "&times=" + times + "&queryKeyWord=" + queryKeyWord + "&count=" + count;
    //$('#id_netty').bootstrapTable("refresh", {url: url});
    $.ajax({
        type:"post",
        url:url,
        dataType:"json",
        success:function(data){
            var tbData=data.object;
            $('#id_netty').bootstrapTable('load', {data: tbData});
            var count=data.count;
            $("#id-netty-record").html("共"+count+"条记录");
        }
    });
}

// netty 树，两级目录
function nettyTreeInit(sysTreeObj, queryFun, indexType) {
    sysTreeObj.jstree({
        'core': {
            "animation": 0,
            "check_callback": true,
            "themes": {"stripes": true},
            "data": {
                "dataType": 'json',
                "url": "netty/getNettyChildrenNode.action?indexType=" + indexType,
                "data": function (node) {
                    var id = node.id;
                    var ref = sysTreeObj.jstree(true);
                    if (id == '#') {
                        return {"id": node.id};
                    } else if (id.split("-").length == 2) {
                        return {"id": node.id, "deviceId": node.text};
                    }
                }
            }
        }
    }).bind('select_node.jstree', function (event, data) {
        var deviceIdOut = "";
        var codeOut = "";

        var node = data.node;
        var id = data.node.id;
        var text = data.node.text;
        var ref = sysTreeObj.jstree(true);
        if (id.split("-").length == 2) {
            deviceIdOut = text;
            codeOut = "";
        } else if (id.split("-").length == 3) {
            var l1Id = ref.get_parent(node);
            var l1Node = ref.get_node(l1Id);
            deviceIdOut = l1Node.text;
            codeOut = text;
        }
        queryFun(deviceIdOut, codeOut);
    });
};

// 批量重传数据
function uploadFiles() {
    // 返回表格中所有选择的行
    var nettyFilesJson = $('#id_netty').bootstrapTable('getAllSelections');
    if (nettyFilesJson.length == 0) {
        common.warning("未选中任何记录!");
        return;
    }
    var str = JSON.stringify(nettyFilesJson);
    // 提交数据重传
    $.ajax({
        type: 'post',
        url: 'netty/uploadNettyFiles.action',
        data: {
            nettyFilesJson: str
        },
        dataType: 'json',
        success: function (data) {
            if (data.msg == "success") {
                common.tips("成功重传了" + data.total + "个文件！");

            }
        },
        error: function () {
            common.tips("重传失败！");
        }
    });

};

// 批量下载文件
function downloadFiles() {
    var nettyFilesJson = $('#id_netty').bootstrapTable('getAllSelections');
    if (nettyFilesJson.length == 0) {
        common.warning("未选中任何记录!");
        return;
    }
    var content = JSON.stringify(nettyFilesJson);
    if (content.length == 0) {
        common.warning("没有符合条件的文件下载!");
        return;
    }
    var form = $("<form>");
    form.attr('style', 'display:none');
    form.attr('target', '');
    form.attr('method', 'post');
    form.attr('action', 'netty/downloadNettyFiles.action');
    var input1 = $('<input>');
    input1.attr('type', 'hidden');
    input1.attr('name', 'content');
    input1.attr('value', content);
    form.append(input1);
    $('body').append(form);
    form.submit();
    form.remove();
};
